package com.globallogic.run;

import com.globallogic.inout.FileOutput;
import com.globallogic.inout.Out;
import com.globallogic.token.TokenProcessor;
import com.globallogic.tokentreebuilder.TokenTreeBuilder;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;


public class JavaDraw {

	public static void main(String[] args) {
		
		JavaDraw javaDrawApp = new JavaDraw();
		String log4jSettingsFile = AppSettings.getLog4jFileName();
				
		if (log4jSettingsFile == null) {
			
			BasicConfigurator.configure();
			
		} else {
			
		    PropertyConfigurator.configure(log4jSettingsFile);
		
		}
		
		logger.info("Start application");
		
		javaDrawApp.init();
		javaDrawApp.run();

		Out.flushAndClose();
		
		logger.info("Stop application");
		
	}
	
	private void readCmdLine(List<String> command){
		
		Scanner input = new Scanner(System.in);
		String[] commandLine;

		command.clear();
		commandLine = input.nextLine().split(" ");
		
		for (int i = 0; i < commandLine.length; i++) {
			
			command.add(commandLine[i]);
			
		}
		
	}
	
	private void init() {
		
		Out.setOutput(new FileOutput(AppSettings.getOutputFileName()));

		//Out.setOutput(new LoggerOutput());
		
	}
	
	private void run() {
		
		List<String> command = new ArrayList<String>();
		TokenProcessor rootProcessor = TokenTreeBuilder.getTokenTree();
		
        do {
        	
        	readCmdLine(command);
        	
        	if (command.get(0).equalsIgnoreCase("exit")) {
        		
        		return;
        		
        	}
        	
        	rootProcessor.handle(command);
		
        } while (true);
		
	}
	
	private static Logger logger = Logger.getLogger(JavaDraw.class);

}
